import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { BasicItemEntity } from "../entities/basic-item.entity";

@Injectable()
export class BasicItemService {
  constructor(
    @InjectRepository(BasicItemEntity)
    private basicItemRepository: Repository<BasicItemEntity>
  ) {}

  /**
   * 根据ID获取物品
   */
  async getItemById(id: number): Promise<BasicItemEntity | null> {
    return await this.basicItemRepository.findOne({
      where: { id, status: 1 },
    });
  }

  /**
   * 获取宝物商店物品列表
   */
  async getTreasureShopItems(
    pageNum: number = 0,
    pageSize: number = 20
  ): Promise<{
    list: BasicItemEntity[];
    total: number;
    pageNum: number;
    pageSize: number;
    totalPages: number;
  }> {
    const [items, total] = await this.basicItemRepository.findAndCount({
      where: { isTreasureShop: 1, status: 1 },
      skip: pageNum * pageSize,
      take: pageSize,
      order: { id: "ASC" },
    });

    return {
      list: items,
      total,
      pageNum,
      pageSize,
      totalPages: Math.ceil(total / pageSize),
    };
  }

  /**
   * 获取经验商店物品列表
   */
  async getExpShopItems(
    pageNum: number = 0,
    pageSize: number = 20
  ): Promise<{
    list: BasicItemEntity[];
    total: number;
    pageNum: number;
    pageSize: number;
    totalPages: number;
  }> {
    const [items, total] = await this.basicItemRepository.findAndCount({
      where: { isExpShop: 1, status: 1 },
      skip: pageNum * pageSize,
      take: pageSize,
      order: { id: "ASC" },
    });

    return {
      list: items,
      total,
      pageNum,
      pageSize,
      totalPages: Math.ceil(total / pageSize),
    };
  }

  /**
   * 根据关键词搜索物品
   */
  async searchItems(
    keyword: string,
    pageNum: number = 0,
    pageSize: number = 20
  ): Promise<{
    list: BasicItemEntity[];
    total: number;
    pageNum: number;
    pageSize: number;
    totalPages: number;
  }> {
    const [items, total] = await this.basicItemRepository.findAndCount({
      where: [
        { name: `%${keyword}%`, status: 1 },
        { description: `%${keyword}%`, status: 1 },
      ],
      skip: pageNum * pageSize,
      take: pageSize,
      order: { id: "ASC" },
    });

    return {
      list: items,
      total,
      pageNum,
      pageSize,
      totalPages: Math.ceil(total / pageSize),
    };
  }

  /**
   * 获取所有启用的物品
   */
  async getAllActiveItems(
    pageNum: number = 0,
    pageSize: number = 20
  ): Promise<{
    list: BasicItemEntity[];
    total: number;
    pageNum: number;
    pageSize: number;
    totalPages: number;
  }> {
    const [items, total] = await this.basicItemRepository.findAndCount({
      where: { status: 1 },
      skip: pageNum * pageSize,
      take: pageSize,
      order: { id: "ASC" },
    });

    return {
      list: items,
      total,
      pageNum,
      pageSize,
      totalPages: Math.ceil(total / pageSize),
    };
  }

  /**
   * 根据等级获取物品
   */
  async getItemsByLevel(
    level: number,
    pageNum: number = 0,
    pageSize: number = 20
  ): Promise<{
    list: BasicItemEntity[];
    total: number;
    pageNum: number;
    pageSize: number;
    totalPages: number;
  }> {
    const [items, total] = await this.basicItemRepository.findAndCount({
      where: { lv: level, status: 1 },
      skip: pageNum * pageSize,
      take: pageSize,
      order: { id: "ASC" },
    });

    return {
      list: items,
      total,
      pageNum,
      pageSize,
      totalPages: Math.ceil(total / pageSize),
    };
  }

  /**
   * 根据ID数组批量获取物品
   */
  async getItemsByIds(ids: number[]): Promise<BasicItemEntity[]> {
    if (!ids || ids.length === 0) {
      return [];
    }

    return await this.basicItemRepository
      .createQueryBuilder("item")
      .where("item.id IN (:...ids)", { ids })
      .andWhere("item.status = :status", { status: 1 })
      .orderBy("item.id", "ASC")
      .getMany();
  }
}
